package com.workcell.workdata.xthings.controller;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseBeltScales;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.BaseBeltScalesService;
import com.workcell.workdata.xthings.service.ManufacturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/basebeltscales")
@Api(value = "basebeltscales", tags = "皮带秤型号管理")
public class BaseBeltScalesController {
	private final BaseBeltScalesService baseBeltScalesService;
	private final ManufacturerService manufacturerService;

	/**
	 * 模糊分页查询
	 *
	 * @param baseVo
	 * @return
	 */
	@ApiOperation(value = "模拟分页查询", notes = "模拟分页查询")
	@PostMapping("/vaguePage")
	public R vaguePage(@RequestBody BaseVo baseVo) {
		return R.ok(baseBeltScalesService.vaguePage(baseVo));
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(baseBeltScalesService.getById(id));
	}

	/**
	 * 新增
	 *
	 * @param baseBeltScales
	 * @return R
	 */
	@ApiOperation(value = "新增功能", notes = "新增")
	@PostMapping
	public R save(@RequestBody BaseBeltScales baseBeltScales) {
		Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseBeltScales.getDeviceManufacturer());
		Assert.notNull(manufacturer, "厂商查询为空");
		baseBeltScales.setManufacturerShortName(manufacturer.getManufacturerShortName());
		baseBeltScales.setManufacturerFullName(manufacturer.getManufacturerFullName());
		baseBeltScales.setManufacturer(manufacturer.getManufacturerFullName());
		baseBeltScales.setManufacturerCode(manufacturer.getManufacturerCode());
		return R.ok(baseBeltScalesService.save(baseBeltScales));
	}

	/**
	 * 修改皮带秤型号基本信息
	 *
	 * @param baseBeltScales
	 * @return R
	 */
	@ApiOperation(value = "修改皮带秤型号", notes = "修改皮带秤型号")
	@PutMapping
	public R updateBaseBeltScales(@RequestBody BaseBeltScales baseBeltScales) {
		Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseBeltScales.getDeviceManufacturer());
		Assert.notNull(manufacturer, "厂商查询为空");
		baseBeltScales.setManufacturerShortName(manufacturer.getManufacturerShortName());
		baseBeltScales.setManufacturerFullName(manufacturer.getManufacturerFullName());
		baseBeltScales.setManufacturer(manufacturer.getManufacturerFullName());
		baseBeltScales.setManufacturerCode(manufacturer.getManufacturerCode());
		return R.ok(baseBeltScalesService.updateBaseBeltScales(baseBeltScales));
	}

	/**
	 * 修改状态
	 *
	 * @param baseBeltScales
	 * @return R
	 */
	@ApiOperation(value = "修改状态", notes = "修改状态")
	@PutMapping("/updateStateById")
	public R updateStateById(@RequestBody BaseBeltScales baseBeltScales) {
		return R.ok(baseBeltScalesService.updateStateById(baseBeltScales));
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除", notes = "通过id删除")
	@DeleteMapping("/{id}")
	public R delById(@PathVariable Long id) {
		return R.ok(baseBeltScalesService.delById(id));
	}

	/**
	 * 导出excel 表格
	 *
	 * @param baseBeltScales 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	public List<BaseBeltScales> export(BaseBeltScales baseBeltScales) {
		return baseBeltScalesService.list(Wrappers.query(baseBeltScales));
	}

}
